

#######################################################################
#######  Appendix A10 - Matching Dataset Creation				  #####
#######################################################################



###############################
####### Setting Up R      #####
###############################

#### Installing Packages ####
## Note: You only have to install packages the first time 
##       that you use them on a computer. 


#install.packages("RcppArmadillo")
#install.packages("MatchIt")
#install.packages("optmatch")

#### Loading Libraries ####
## Note: You have to install libraries every time you open R. 

library(foreign)
library(MatchIt)

###############################
####### Loading Data      #####
###############################


#### Loading Data ####
## Note: Before loading the data, you must change your directory.
##       To do this on a mac, go to the "sc" tab, then click change
##       directory. Then browse through a find the right folder on 
##       your computer. 



#######################################################################
#######  Matching		- ALl 			  #####
#######################################################################

data1 <- read.dta("Data_OPO_Master.dta")
dim(data1)
names(data1)

data1 <- data1[data1$use==1,]
dim(data1)

### Subsetting the Data ####
myvars <- c("sc_cert_granted","panel_odd_party_out","respondent_odd_party_out","petitioner_odd_party_out","coa_enbanc","coa_panel_dissent","coa_lc_reversed","dismissed","sc_cert_sg_petitioner","sc_cert_sg_respondent","sc_pet_corp","sc_resp_corp","sc_pet_pro_se","sc_pet_vet_atty_ln","sc_term","coa_circuit" )
subdata1 <- data1[myvars]
subdata1 <- na.omit(subdata1)
dim(subdata1)

### Matching -  ###

set.seed(12345)
m.out1 <- matchit(petitioner_odd_party_out ~ panel_odd_party_out +respondent_odd_party_out+coa_enbanc+coa_panel_dissent+coa_lc_reversed+dismissed+sc_cert_sg_petitioner+sc_cert_sg_respondent+sc_pet_corp+sc_resp_corp+sc_pet_pro_se+sc_pet_vet_atty_ln+as.factor(sc_term)+as.factor(coa_circuit),data=subdata1, method="nearest",caliper=0.2)
summary(m.out1)
### Saving the Data ###
m.out1 <- match.data(m.out1) 
write.dta(m.out1, "Data_OPO_Matched_All.dta") 



#######################################################################
#######  Matching		- Civil			  #####
#######################################################################

data1 <- read.dta("Data_OPO_Master.dta")
dim(data1)
names(data1)

data1 <- data1[data1$use==1,]
data1 <- data1[data1$civil==1,]

dim(data1)

### Subsetting the Data ####
myvars <- c("sc_cert_granted","panel_odd_party_out","respondent_odd_party_out","petitioner_odd_party_out","coa_enbanc","coa_panel_dissent","coa_lc_reversed","dismissed","sc_cert_sg_petitioner","sc_cert_sg_respondent","sc_pet_corp","sc_resp_corp","sc_pet_pro_se","sc_pet_vet_atty_ln","sc_term","coa_circuit" )
subdata1 <- data1[myvars]
subdata1 <- na.omit(subdata1)
dim(subdata1)

### Matching ###

set.seed(12345)
m.out1 <- matchit(petitioner_odd_party_out ~ panel_odd_party_out +respondent_odd_party_out+coa_enbanc+coa_panel_dissent+coa_lc_reversed+dismissed+sc_cert_sg_petitioner+sc_cert_sg_respondent+sc_pet_corp+sc_resp_corp+sc_pet_pro_se+sc_pet_vet_atty_ln+as.factor(sc_term)+as.factor(coa_circuit),data=subdata1, method="nearest",caliper=0.2)
summary(m.out1)
### Saving the Data ###
m.out1 <- match.data(m.out1) 
write.dta(m.out1, "Data_OPO_Matched_Civil.dta") 

#######################################################################
#######  Matching		- Criminal		  #####
#######################################################################

data1 <- read.dta("Data_OPO_Master.dta")
dim(data1)
names(data1)

data1 <- data1[data1$use==1,]
data1 <- data1[data1$criminal==1,]

dim(data1)

### Subsetting the Data ####
myvars <- c("sc_cert_granted","panel_odd_party_out","respondent_odd_party_out","petitioner_odd_party_out","coa_enbanc","coa_panel_dissent","coa_lc_reversed","dismissed","sc_cert_sg_petitioner","sc_cert_sg_respondent","sc_pet_corp","sc_resp_corp","sc_pet_pro_se","sc_pet_vet_atty_ln","sc_term","coa_circuit" )
subdata1 <- data1[myvars]
subdata1 <- na.omit(subdata1)
dim(subdata1)
### Matching - Model 4  ###

set.seed(12345)
m.out1 <- matchit(petitioner_odd_party_out ~ panel_odd_party_out +respondent_odd_party_out+coa_enbanc+coa_panel_dissent+coa_lc_reversed+dismissed+sc_cert_sg_petitioner+sc_cert_sg_respondent+sc_pet_corp+sc_resp_corp+sc_pet_pro_se+sc_pet_vet_atty_ln+as.factor(sc_term)+as.factor(coa_circuit),data=subdata1, method="nearest",caliper=0.2)
summary(m.out1)
### Saving the Data ###
m.out1 <- match.data(m.out1) 
write.dta(m.out1, "Data_OPO_Matched_Criminal.dta") 

